package com.andy.nature.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.andy.common.model.Label
import com.andy.nature.BR
import com.andy.nature.databinding.AdapterLabelItemBlueBinding

class DetailLabelAdapter : RecyclerView.Adapter<DetailLabelAdapter.BottomViewHolder>() {

    private var mContext: Context? = null
    private var mList = mutableListOf<Label>()

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        mContext = recyclerView.context
    }

    fun setData(list: MutableList<Label>) {
        mList.clear()
        mList.addAll(list)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BottomViewHolder {
        val binding = AdapterLabelItemBlueBinding.inflate(LayoutInflater.from(mContext), parent, false)
        return BottomViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BottomViewHolder, position: Int) {
        val data = mList[position]
        DataBindingUtil.bind<AdapterLabelItemBlueBinding>(holder.itemView)?.let {
            it.setVariable(BR.blueLabelItem, data)
            it.executePendingBindings()
        }
    }

    class BottomViewHolder(val binding: AdapterLabelItemBlueBinding) : RecyclerView.ViewHolder(binding.root)
}